package controllers

import (
	"strconv"
	"xiaoshuo/models"

	"github.com/gin-gonic/gin"
)

type XiaoHuaController struct{}

func (xiaohua XiaoHuaController) GetList(c *gin.Context) {
	ReturnSuccess(c, 0, "success", "a", 1)
}

func (xiaohua XiaoHuaController) AddXiaoHua(c *gin.Context) {
	content := c.DefaultPostForm("content", "")
	id, err := models.AddXiaoHua(content)
	if err != nil {
		ReturnError(c, 4001, "添加失败")
		return
	}

	ReturnSuccess(c, 0, "success", id, 1)
}

func (xiaohua XiaoHuaController) UpdateXiaoHua(c *gin.Context) {
	content := c.DefaultPostForm("content", "")
	idStr := c.DefaultPostForm("id", "0")
	id, _ := strconv.Atoi(idStr)

	models.UpdateXiaoHua(id, content)
	ReturnSuccess(c, 0, "修改成功", id, 1)
}

func (xiahua XiaoHuaController) DeleteXiaoHua(c *gin.Context) {
	idStr := c.DefaultPostForm("id", "0")
	id, _ := strconv.Atoi(idStr)

	err := models.DeleteXiaohua(id)
	if err != nil {
		ReturnError(c, 4002, "删除失败")
		return
	}
	ReturnSuccess(c, 0, "删除成功", id, 1)
}

func (xiaohua XiaoHuaController) GetxiaoHuaList(c *gin.Context) {
	xiaohuas, err := models.GetXiaoHuaList()
	if err != nil {
		ReturnError(c, 4003, "获取数据为空")
		return
	}
	ReturnSuccess(c, 0, "获取数据成功", xiaohuas, 1)
}
